/**
 * VehicleInsuranceTypeAction.java
 */
package com.prs.crm.action.oa;

import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.meeting.MeetSource;
import com.prs.crm.domain.oa.VehicleInsuranceType;
import com.prs.crm.service.oa.VehicleInsuranceTypeService;

/**
 * @author ZhuDongMei
 * @E-mail:1223984339@qq.com
 * @qq:1223984339
 * @version 创建时间：2011-3-16 上午11:12:28 简单说明
 */
/**
 * @author Administrator
 *
 */
public class VehicleInsuranceTypeAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private PaginationSupport pagination;
	private VehicleInsuranceTypeService vehicleInsuranceTypeService;
	private VehicleInsuranceType vehicleInsuranceType;
	private Integer[] vehicleInsuranceTypeIds;
	private TreeNode root;
	public TreeNode getRoot() {
		return root;
	}
	public void setRoot(TreeNode root) {
		this.root = root;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public VehicleInsuranceTypeService getVehicleInsuranceTypeService() {
		return vehicleInsuranceTypeService;
	}
	public void setVehicleInsuranceTypeService(
			VehicleInsuranceTypeService vehicleInsuranceTypeService) {
		this.vehicleInsuranceTypeService = vehicleInsuranceTypeService;
	}
	public VehicleInsuranceType getVehicleInsuranceType() {
		return vehicleInsuranceType;
	}
	public void setVehicleInsuranceType(VehicleInsuranceType vehicleInsuranceType) {
		this.vehicleInsuranceType = vehicleInsuranceType;
	}
	public Integer[] getVehicleInsuranceTypeIds() {
		return vehicleInsuranceTypeIds;
	}
	public void setVehicleInsuranceTypeIds(Integer[] vehicleInsuranceTypeIds) {
		this.vehicleInsuranceTypeIds = vehicleInsuranceTypeIds;
	}
	@Action(value = "loadVehicleInsuranceTypTree", results = { @Result(name = "success", type = "json", params = {
			"root", "root" }) })
	public String loadVehicleInsuranceTypTree() {
		root = new TreeNode();
		root.setId("-1");

		TreeNode firstNode = new TreeNode();
		firstNode.setId("0");
		firstNode.setText("险种选择");

		Collection<VehicleInsuranceType> vehicleInsuranceTypes = this.getVehicleInsuranceTypeService().getRootVehicleInsuranceTypes();

		for (VehicleInsuranceType vehicleInsuranceType : vehicleInsuranceTypes) {
			TreeNode node = new TreeNode();
			node.setText(vehicleInsuranceType.getName());
			node.setId("type-" + vehicleInsuranceType.getId().toString());
			firstNode.addChildNode(node);
			addDepartNode(node, vehicleInsuranceType);
			
		}

		root.addChildNode(firstNode);
		return SUCCESS;
	}
	private void addDepartNode(TreeNode parent, VehicleInsuranceType vehicleInsuranceType) {
		for (VehicleInsuranceType type : vehicleInsuranceType.getChilds()) {
			TreeNode node = new TreeNode();
			node.setText(type.getName());
			node.setId("types-" + type.getId().toString());
			parent.addChildNode(node);
			addDepartNode(node, type);
			
		}
	}
	@Action("listVehicleInsuranceType")
	public String listVehicleInsuranceType(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				VehicleInsuranceType.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getVehicleInsuranceTypeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("editVehicleInsuranceType")
	public String editVehicleInsuranceType(){
		return SUCCESS;
	}
	@Action(value = "saveVehicleInsuranceType", results = {
			@Result(name = "success", type = "redirect", location = "listVehicleInsuranceType"),
			@Result(name = "error", type = "chain", location = "editVehicleInsuranceType") })
	public String saveVehicleInsuranceType(){
		if("".equals(vehicleInsuranceType.getName().trim())){
			this.addActionError("险种名称不能为空！");
			return ERROR;
		}
		this.getVehicleInsuranceTypeService().save(vehicleInsuranceType);
		return SUCCESS;
	}
	@Action(value = "removeVehicleInsuranceType", results = {
			@Result(name = "success", type = "redirect", location = "listVehicleInsuranceType"),
			@Result(name = "error", type = "chain", location = "listVehicleInsuranceType") })
	public String removeVehicleInsuranceType(){
		if(vehicleInsuranceTypeIds==null){
			this.addActionError("请选择操作行！");
			return ERROR;
		}
		this.getVehicleInsuranceTypeService().batchRemove(vehicleInsuranceTypeIds);
		return SUCCESS;
	}
}
